package org.hcf.common;

import lombok.Data;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Arrays;

@Data
public class UserSigInfo implements Serializable {
    long userId;
    String userSig;
    long timeout;

    public byte[] toBytes() {
        byte[] content = userSig.getBytes();
        ByteBuffer buffer = ByteBuffer.allocate(16 + content.length);
        buffer.putLong(userId).putLong(timeout).put(content);
        return buffer.array();
    }

    public static UserSigInfo from(byte[] bs) {
        if (bs == null || bs.length <= 8) return null;
        ByteBuffer buffer = ByteBuffer.wrap(bs);
        UserSigInfo info = new UserSigInfo();
        info.userId = buffer.getLong();
        info.timeout = buffer.getLong();
        byte[] cs = Arrays.copyOfRange(bs, 16, bs.length);
        info.userSig = new String(cs);
        return info;
    }
}
